package com.pass.util.dialog;

import android.content.DialogInterface;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.pass.util.R;

import java.security.KeyStore;

/**
 * 进度条对话框，一个progressbar 一个提示信息。点击外围可以进行取消
 */
public class ProgressDialog extends BaseDialogFragment {

    public interface OnProgressListener {
        void onCancelled();
    }

    private TextView contentView;
    private TextView titleView;
    private String contentStr;
    private String titleStr;
    private ProgressBar progressBar;
    private RelativeLayout mTotalLayout;

    private OnProgressListener callback;


    public ProgressDialog(String titleStr, String contentStr, OnProgressListener callback) {
        super(DialogPosition.CENTER, false);
        this.titleStr = titleStr;
        this.contentStr = contentStr;
        this.callback = callback;
    }

    public void updateShownMsg(int progress, String detailMsg) {
        progressBar.setProgress(progress);
        contentView.setText(detailMsg);
    }


    @Override
    protected int getLayoutId() {
        return R.layout.dlg_progress;
    }

    @Override
    protected int getDialogStyleId() {
        return R.style.MyDialogStyle;
    }

    @Override
    protected void initView(View rootView) {
        contentView = rootView.findViewById(R.id.tv_content);
        titleView = rootView.findViewById(R.id.tv_title);
        progressBar = rootView.findViewById(R.id.progress);
        mTotalLayout = rootView.findViewById(R.id.rl_total);
    }

    @Override
    protected void initData() {
        titleView.setText(this.titleStr);
        contentView.setText(this.contentStr);
    }

    @Override
    protected void initListener() {
        mTotalLayout.setOnClickListener(v -> callback.onCancelled());
    }

    @Override
    public void onCancel(@NonNull DialogInterface dialog) {
        if (null != callback) {
            callback.onCancelled();
        }
        super.onCancel(dialog);
    }

    @Override
    public void onStart() {
        super.onStart();
        Window window = getDialog().getWindow();
        window.setBackgroundDrawableResource(R.color.translucent);
        WindowManager.LayoutParams attributes = window.getAttributes();
        DisplayMetrics displayMetrics = getResources().getDisplayMetrics();
        attributes.width = displayMetrics.widthPixels / 2;
        attributes.height = displayMetrics.heightPixels / 3;
//        attributes.gravity = Gravity.CENTER;
        window.setAttributes(attributes);

    }

    @Override
    public void onDismiss(@NonNull DialogInterface dialog) {
        super.onDismiss(dialog);

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }
}
